package com.ruoyi.hr.controller.hr.entryexitmanage.entrymanage;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.CompanysService;
import com.ruoyi.hr.service.DepartmentsService;
import com.ruoyi.hr.service.JobsService;
import com.ruoyi.hr.service.entryexitmanage.EntryExitManageService;
import com.ruoyi.hr.service.entryexitmanage.entrymanage.NewEmpRegisterService;
import com.ruoyi.hr.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zeo
 * @explain 新员工入职管理
 * @date 2020/2/27
 */
@Controller
@RequestMapping("/entryManage/newEmpRegister")
public class EntryManageController extends BaseController {

    @Autowired
    NewEmpRegisterService newEmpRegisterService;

    @Autowired
    AuthorityService authorityService;

    @Autowired
    private CompanysService companysService;

    @Autowired
    EntryExitManageService entryExitManageService;

    @Autowired
    JobsService jobsService;

    @Autowired
    private DepartmentsService departmentsService;

    private String prefix = "hr/entryexitmanage/entrymanage";

    /**
     * 跳转分页页面
     */
    @GetMapping()
    public String entryExitManage() {
        return prefix + "/entryManage";
    }

    /**
     * 添加员工跳转
     *
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        //公司
        mmap.put("companys", companysService.findAllCompanys(null));
        //部门
        mmap.put("departments", departmentsService.findDep((long) 33));
        //签约单位
        mmap.put("contract", entryExitManageService.getContract());

        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(newEmpRegisterService.addNewEmp(map));
    }

    @GetMapping("edit/{id}")
    public String welfareOpen(@PathVariable("id") int id, ModelMap mmap) {
        //根据ID取单条数据
        Map<String, Object> map = newEmpRegisterService.finEmpDataById(id);
        Long compId = Long.valueOf(String.valueOf(map.get("compIId"))).longValue();
        Long depId = Long.valueOf(String.valueOf(map.get("DepIId"))).longValue();

        mmap.put("emp", map);
        //公司
        mmap.put("companys", companysService.findAllCompanys(null));
        //部门
        mmap.put("departments", departmentsService.findDep((long) compId));
        //岗位
        mmap.put("jobs", jobsService.findJobs((long) depId));
        //签约单位
        mmap.put("contract", entryExitManageService.getContract());
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(newEmpRegisterService.editNewEmp(map));
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(newEmpRegisterService.remove(ids));
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(newEmpRegisterService.findEmpData(map, authority));
    }

    @GetMapping("/getReportTo")
    public String getReportTo(String type, ModelMap mmap) {
        mmap.put("type", type);
        return prefix + "/getReportTo";
    }

    @GetMapping("/getWorkCity")
    public String getWorkCity() {
        return prefix + "/getWorkCity";
    }

    @GetMapping("/getWorkCityData")
    @ResponseBody
    public List<Ztree> getWorkCityData() {
        List<Ztree> ztrees = this.initZtree(newEmpRegisterService.findAllTree());
        return ztrees;
    }

    public List<Ztree> initZtree(List<Map<String, Object>> deptList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        Ztree ztree;
        for (Map dept : deptList) {
            ztree = new Ztree();
            ztree.setId(Long.parseLong(dept.get("id").toString()));
            ztree.setpId(Long.parseLong(dept.get("pid").toString()));
            ztree.setName(dept.get("name").toString());
            ztree.setTitle(dept.get("name").toString());
            ztree.setChecked(true); //默认全部展开
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @PostMapping("/getJobGrade")
    @ResponseBody
    public String getJobGrade(String jobId) {
        return newEmpRegisterService.getJobGrade(jobId);
    }

    @PostMapping("/getGenderAndBirthDay")
    @ResponseBody
    public Map getGenderAndBirthDay(String CertType, String CertNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        //找性别
        String gender = newEmpRegisterService.getGender(CertType, CertNo);
        //找出生日期
        String BirthDay = newEmpRegisterService.getBirthDay(CertType, CertNo);
        map.put("gender", gender);
        map.put("BirthDay", BirthDay);
        return map;
    }

    @GetMapping("/entryBackground")
    public String entryBackground(String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/entryBackground";
    }


    /**
     * 数据确认,取消确认,执行
     */
    @PostMapping("/procedure")
    @ResponseBody
    public BaseResult procedure(String allId, String procedure) {
        return newEmpRegisterService.procedure(allId, procedure);
    }


    @GetMapping("/entryHistory")
    public String entryHistory() {
        return prefix + "/entryHistory";
    }

    @RequestMapping("/entryHistory")
    @ResponseBody
    public TableDataInfo entryHistory(@RequestParam Map map) {
        Authority authority = authorityService.findAuthorAndIsAdmin();
        startPage();
        return getDataTable(entryExitManageService.entryHistory(map, authority));
    }

}
